using System;
using System.Data;
using System.Xml;
using System.Web.UI;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;

namespace Umbraco.Ecommerce.WebShop.DataTypes
{
	/// <summary>
	/// An datatype addon to the umbraco interface (added to the "Customer" Membertype), that handles 
	/// the listing of orders placed by the current customer/member
	/// 
	/// Uses the usercontrol: "/usercontrols/recentorders.ascx" as UI
	/// </summary>
	public class ListOrderDataType : System.Web.UI.WebControls.WebControl, umbraco.interfaces.IDataType, umbraco.interfaces.IData, umbraco.interfaces.IDataEditor
	{
		private UserControls.RecentOrders c;

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad (e);
			try 
			{	
				c = (UserControls.RecentOrders) this.Page.LoadControl("/usercontrols/recentorders.ascx");
				c.ThisMember = new umbraco.cms.businesslogic.member.Member(int.Parse(umbraco.helper.Request("id")));
				this.Controls.Add(c);
			} 
			catch (Exception ex) 
			{
				this.Controls.Add(new LiteralControl("Error loading control<br /> " + ex.ToString()));
			}
		}

		#region IDataType Members

		public umbraco.interfaces.IData Data
		{
			get
			{
				return this;
			}
		}

		public Guid Id
		{
			get
			{
				return new Guid ("8C8DFC00-EC7D-46f9-ABE4-EB65B6653F8D");
			}
		}

		public umbraco.interfaces.IDataEditor DataEditor
		{
			get
			{
				return this;
			}
		}

		private int _datatypedefinitionid = 0;
		public int DataTypeDefinitionId
		{
			get
			{
				return _datatypedefinitionid;
			}
			set
			{
				_datatypedefinitionid = value;
			}
		}

		public string DataTypeName
		{
			get
			{
				return "Customer orders";
			}
		}

		public umbraco.interfaces.IDataPrevalue PrevalueEditor
		{
			get
			{
				return null;
			}
		}

		#endregion

		#region IData Members

		public System.Xml.XmlNode ToXMl(System.Xml.XmlDocument d)
		{
			XmlNode retval = d.CreateTextNode(val.ToString());
			return retval;
		}

		object val = "";

		public object Value
		{
			get
			{
				return val;
			}
			set
			{
				if (value == null)
					SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN, CommandType.Text, "update cmsPropertyData set dataNText = NULL where id = " + _propertyId);
				else
					SqlHelper.ExecuteNonQuery(umbraco.GlobalSettings.DbDSN, CommandType.Text, "update cmsPropertyData set dataNText = @value where id = " + _propertyId, new SqlParameter("@value", value) );
				val = value;
			}
		}

		public void MakeNew(int PropertyId)
		{
			// No makenew is required since this datatype uses the default umbraco storage.
		}

		private int _propertyId = 0;

		// When the property id is set, the data initializes.
		public int PropertyId
		{
			set
			{
				_propertyId = value;
				val = SqlHelper.ExecuteScalar(umbraco.GlobalSettings.DbDSN, CommandType.Text,"Select dataNText from cmsPropertyData where id = " + value);
			}
		}

		public void Delete()
		{
			// No delete is required since this datatype uses the default umbraco storage
		}

		#endregion

		#region IDataEditor Members

		public bool ShowLabel
		{
			get
			{
				return false;
			}
		}

		
		public System.Web.UI.Control Editor
		{
			get
			{
				return this;
			}
		}

		public void Save()
		{
		}

		public bool TreatAsRichTextEditor
		{
			get
			{
				return false;
			}
		}

		#endregion
	}
}
